home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / win / pascal / ansioem.exe / ANSIOEM.C < prev    next >
C/C++ Source or Header  |  1992-07-25  |  15KB  |  474 lines

  1. /*---------------------------------------
  2.    ANSIOEM.C -- Ansi & Oem Characters.
  3.                  (c) Robert Digsby, 1992
  4.   ---------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. #define DIVISIONSX 32
  9. #define DIVISIONSY  8
  10. #define ANSI        0
  11. #define OEM         1
  12. #define STATUS      2
  13.  
  14. long FAR PASCAL WndProc        (HWND, WORD, WORD, LONG) ;
  15. long FAR PASCAL AnsiOemWndProc (HWND, WORD, WORD, LONG) ;
  16. long FAR PASCAL StatusWndProc  (HWND, WORD, WORD, LONG) ;
  17.  
  18. HWND  hwndChild [3] ;
  19. char  szAnsiOemClass[] = "AnsiOem_Child" ;
  20. char  szStatusClass[]  = "Status_Child" ;
  21. short cxBlock, cyBlock, cxChar, cyChar ;
  22.  
  23. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  24.                     LPSTR  lpszCmdLine, int nCmdShow)
  25.      {
  26.      static char szAppName[] = "AnsiOem" ;
  27.      HWND        hwnd ;
  28.      MSG         msg ;
  29.      WNDCLASS    wndclass ;
  30.  
  31.      if (!hPrevInstance) 
  32.           {
  33.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  34.       wndclass.lpfnWndProc   = WndProc ;
  35.           wndclass.cbClsExtra    = 0 ;
  36.           wndclass.cbWndExtra    = 0 ;
  37.           wndclass.hInstance     = hInstance ;
  38.           wndclass.hIcon         = NULL ;
  39.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  40.           wndclass.hbrBackground = COLOR_WINDOW + 1 ;
  41.           wndclass.lpszMenuName  = NULL ;
  42.           wndclass.lpszClassName = szAppName ;
  43.  
  44.           RegisterClass (&wndclass) ;
  45.  
  46.       wndclass.lpfnWndProc   = AnsiOemWndProc ;
  47.       wndclass.cbWndExtra    = sizeof (DWORD) ;
  48.           wndclass.hIcon         = NULL ;
  49.       wndclass.lpszClassName = szAnsiOemClass ;
  50.  
  51.       RegisterClass (&wndclass) ;
  52.  
  53.       wndclass.lpfnWndProc   = StatusWndProc ;
  54.       wndclass.cbWndExtra    = 0 ;
  55.           wndclass.hIcon         = NULL ;
  56.       wndclass.lpszClassName = szStatusClass ;
  57.  
  58.       RegisterClass (&wndclass) ;
  59.       }
  60.              
  61.      hwnd = CreateWindow (szAppName, "Ansi & Oem Charaters",
  62.                          WS_OVERLAPPEDWINDOW,
  63.                          CW_USEDEFAULT, CW_USEDEFAULT,
  64.                          CW_USEDEFAULT, CW_USEDEFAULT,
  65.                          NULL, NULL, hInstance, NULL) ;
  66.  
  67.      ShowWindow (hwnd, nCmdShow) ;
  68.      UpdateWindow (hwnd) ;
  69.  
  70.      while (GetMessage (&msg, NULL, 0, 0))
  71.           {
  72.           TranslateMessage (&msg) ;
  73.           DispatchMessage (&msg) ;
  74.           }
  75.      return msg.wParam ;
  76.      }
  77.  
  78. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  79.      {
  80.      HDC          hdc ;
  81.      TEXTMETRIC   tm ;
  82.      short        cxClient, cyClient, cxAnsiPos, cyAnsiPos, cxScreen,
  83.           cyScreen, cxSize, cySize ;
  84.  
  85.      switch (message)
  86.           {
  87.       case WM_CREATE :
  88.            hdc = GetDC (hwnd) ;
  89.  
  90.                SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  91.  
  92.                GetTextMetrics (hdc, &tm) ;
  93.                cxChar = tm.tmAveCharWidth ;
  94.                cyChar = tm.tmHeight + tm.tmExternalLeading ;
  95.                cxBlock = cxChar * 2 ;
  96.                cyBlock = cyChar * 1.3; 
  97.  
  98.                ReleaseDC (hwnd, hdc) ;
  99.  
  100.            cxScreen = GetSystemMetrics (SM_CXSCREEN) ;
  101.            cyScreen = GetSystemMetrics (SM_CYSCREEN) ;
  102.            cxSize = cxBlock * (DIVISIONSX + 2) ;
  103.            cySize = cyBlock * ((DIVISIONSY * 2) + 5) ;
  104.  
  105.            MoveWindow (hwnd, (cxScreen - cxSize) / 2,
  106.                 (cyScreen - cySize) / 2,
  107.             cxSize, cySize, FALSE) ;
  108.                           
  109.            hwndChild[ANSI] = CreateWindow (szAnsiOemClass, NULL,
  110.             WS_CHILDWINDOW | WS_VISIBLE,
  111.             0, 0, 0, 0,
  112.             hwnd, ANSI,
  113.             GetWindowWord (hwnd, GWW_HINSTANCE), NULL) ;
  114.  
  115.            hwndChild[OEM] = CreateWindow (szAnsiOemClass, NULL,
  116.             WS_CHILDWINDOW | WS_VISIBLE,
  117.             0, 0, 0, 0,
  118.                     hwnd, OEM,
  119.             GetWindowWord (hwnd, GWW_HINSTANCE), NULL) ;
  120.  
  121.            hwndChild[STATUS] = CreateWindow (szStatusClass, NULL,
  122.             WS_CHILDWINDOW | WS_VISIBLE,
  123.             0, 0, 0, 0,
  124.             hwnd, STATUS,
  125.             GetWindowWord (hwnd, GWW_HINSTANCE), NULL) ;
  126.  
  127.                return 0 ;
  128.  
  129.       case WM_SIZE :
  130.            cxClient = LOWORD (lParam) ;
  131.            cyClient = HIWORD (lParam) ;
  132.  
  133.            cxAnsiPos = max (cxBlock,
  134.                 (cxClient - (cxBlock * DIVISIONSX)) / 2) ;
  135.            cyAnsiPos = max (cyBlock,
  136.                 ((cyClient - (cyBlock * ((DIVISIONSY * 2) + 2))) / 2)) ;
  137.                
  138.            MoveWindow (hwndChild[ANSI], cxAnsiPos, cyAnsiPos,
  139.            cxBlock * DIVISIONSX, cyBlock * DIVISIONSY, TRUE) ;
  140.  
  141.            MoveWindow (hwndChild[OEM],
  142.            cxAnsiPos,
  143.            cyAnsiPos + (cyBlock * DIVISIONSY) + (cyBlock * 2), 
  144.            cxBlock * DIVISIONSX, cyBlock * DIVISIONSY, TRUE) ;
  145.  
  146.            MoveWindow (hwndChild[STATUS],
  147.            cxAnsiPos,
  148.            cyAnsiPos + (cyBlock * DIVISIONSY) + (cyBlock / 2),
  149.            cxBlock * DIVISIONSX, cyBlock, TRUE) ;
  150.  
  151.            return 0 ;
  152.  
  153.           case WM_LBUTTONDOWN :
  154.            MessageBeep (0) ;
  155.                return 0 ;
  156.                  
  157.       case WM_SETFOCUS :
  158.                SetFocus (hwndChild[ANSI]) ;
  159.                return 0 ;
  160.  
  161.           case WM_DESTROY :
  162.            PostQuitMessage (0) ;
  163.                return 0 ;
  164.  
  165.       }
  166.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  167.      }
  168.  
  169.  
  170. long FAR PASCAL AnsiOemWndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  171.      {
  172.      short         x, y, cxPos, cyPos, cPos, xStart, xEnd, yStart, yEnd, n ;
  173.      unsigned char sBuffer[1], sConvert[1], sConverted[1] ;
  174.      HDC           hdc ;
  175.      PAINTSTRUCT   ps ;
  176.      RECT          rect ;
  177.      HBRUSH        hBrush ;
  178.      DWORD         dwPrevColor, dwPrevText ;
  179.  
  180.      switch (message)
  181.           {
  182.       case WM_CREATE:
  183.            SetWindowWord (hwnd, 0, 0) ;
  184.            SetWindowWord (hwnd, 2, 0) ;
  185.            return 0 ;
  186.  
  187.           case WM_PAINT:
  188.            hdc = BeginPaint (hwnd, &ps) ;
  189.                        
  190.                if (ANSI == GetWindowWord (hwnd, GWW_ID) )
  191.             SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  192.            else
  193.             SelectObject (hdc, GetStockObject (OEM_FIXED_FONT)) ;
  194.                                        
  195.            Rectangle (hdc, 0, 0, cxBlock * DIVISIONSX,
  196.                 cyBlock * DIVISIONSY) ;
  197.  
  198.                xStart = ps.rcPaint.left / cxBlock ;
  199.            xEnd   = ps.rcPaint.right / cxBlock ;
  200.            yStart = ps.rcPaint.top / cyBlock ;
  201.            yEnd   = ps.rcPaint.bottom / cyBlock ;
  202.  
  203.            for (x = xStart; x <= xEnd; x++)
  204.             {
  205.             MoveTo (hdc, x * cxBlock, 0) ;
  206.             LineTo (hdc, x * cxBlock, cyBlock * DIVISIONSY) ;
  207.             }
  208.  
  209.            for (y = yStart; y <= yEnd; y++)
  210.             {
  211.             MoveTo (hdc, 0, y * cyBlock) ;
  212.             LineTo (hdc, cxBlock * DIVISIONSX, y * cyBlock) ;
  213.             }
  214.  
  215.                SetTextAlign (hdc, TA_CENTER) ;
  216.            SetTextColor(hdc, GetSysColor (COLOR_WINDOWTEXT)) ;
  217.            SetBkColor  (hdc, GetSysColor (COLOR_WINDOW)) ;
  218.  
  219.  
  220.                for (y = yStart; y <= yEnd; y++)
  221.                 for (x = xStart; x <= xEnd; x++)
  222.                          {
  223.                  if (y == GetWindowWord (hwnd, 2) &&
  224.                       x == GetWindowWord (hwnd, 0))
  225.                   {
  226.                   SetRect (&rect, x * cxBlock, y * cyBlock,
  227.                                   (x * cxBlock) + cxBlock,
  228.                                    (y * cyBlock) + cyBlock) ;
  229.                   if (hwnd == GetFocus())
  230.                                    {
  231.                    hBrush = CreateSolidBrush
  232.                         (GetSysColor (COLOR_HIGHLIGHT) ) ;
  233.                    SetTextColor(hdc,
  234.                          GetSysColor (COLOR_HIGHLIGHTTEXT)) ;
  235.                    SetBkColor (hdc,
  236.                                          GetSysColor (COLOR_HIGHLIGHT)) ;
  237.                                    }
  238.                       else
  239.                                    {
  240.                    hBrush = CreateSolidBrush
  241.                         (GetSysColor (COLOR_BTNFACE)) ;
  242.                    SetBkColor (hdc,
  243.                     GetSysColor (COLOR_BTNFACE)) ;
  244.                                    }
  245.                   FillRect (hdc, &rect, hBrush) ;
  246.                               DeleteObject (hBrush) ;
  247.                   }
  248.                  sBuffer[0] = (y * DIVISIONSX) + x ;
  249.                          TextOut (hdc, (x * cxBlock) + cxChar,
  250.                       (y * cyBlock) + (cyBlock - cyChar) / 2, 
  251.                   sBuffer, 1) ;
  252.                  if (y == GetWin